﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetComponentsDx.Browsers;
using ProgNetComponents.Classes;

namespace Wydatki.Setup.Praca
{
    public partial class FormKwotyZus : MyBaseSqlCeGridForm
    {
        private FormKwotyZus()
        {
            InitializeComponent();
        }

        private int Rok;

        public FormKwotyZus(int rok)
        {
            InitializeComponent();
            Rok = rok;
            reRok.EditValue = rok;
            stpKwotyZusDataSet.STP_KWOTY_ZUS.STP_KWZ_ROKColumn.DefaultValue = rok;
        }

        protected override bool GetRecords()
        {
            selectDataProvider1.GetSettings(this).Parameters["ROK"].Value = Rok;
            bool res = base.GetRecords();
            if (res && stpKwotyZusDataSet.STP_KWOTY_ZUS.Rows.Count == 0)
                InitNewData();
            return res;
        }

        protected override void EditClick()
        {
            base.EditClick();
            if (stpKwotyZusDataSet.STP_KWOTY_ZUS.Rows.Count == 0)
            {
                InitNewData();
            }
        }

        private void InitNewData()
        {
            if (modeProvider1.CurrentMode != ProgNetComponentsDx.Enums.FormMode.Read &&
                modeProvider1.CurrentMode != ProgNetComponentsDx.Enums.FormMode.View)
            {
                DataRow r;
                for (int i=1; i < 13; i++)
                {
                    r = stpKwotyZusDataSet.STP_KWOTY_ZUS.NewRow();
                    r["STP_KWZ_MIESIAC"] = i;
                    stpKwotyZusDataSet.STP_KWOTY_ZUS.Rows.Add(r);
                }
            }
        }

        private void btnSetAll_Click(object sender, EventArgs e)
        {
            object doZaplaty = DBNull.Value;
            object doOdliczenia = DBNull.Value;
            if(textEdit1.EditValue.IsNotNull())
                doZaplaty = textEdit1.EditValue.AsDecimal();
             if(textEdit2.EditValue.IsNotNull())
                doOdliczenia = textEdit2.EditValue.AsDecimal();

            foreach (DataRow r in stpKwotyZusDataSet.STP_KWOTY_ZUS.Rows)
            {
                r["STP_KWZ_DO_ZAPLATY"] = doZaplaty;
                r["STP_KWZ_DO_ODLICZENIA"] = doOdliczenia;
            }
            changeDetectorProvider1.RefreshObject(gvMain);
        }

        private void bbiOtworz_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Browser b = BrowserCache.GetBrowser(Init.InitFormBrowserNames.Glowna.LataPodatkowe);
            ObjectDialogResult dlg = b.ShowDialog();
            if (dlg.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                Rok = dlg.Value.AsInt();
                GetRecords();
            }
        }
    }
}
